package com.querie.algorithms;

import java.util.HashMap;
import java.util.Map;


import com.querie.model.beans.Query;
import com.querie.util.Util;

public class FragmentRankRelative implements FragmentRank {
	
	public Map<Long,Double> getFragmentRank(Query oQuery) {
		Map<Long, Double> hmFragmentRank = new HashMap<Long, Double>();
		int i;
		
		long[] arFragmentId = Util.convertStringToLongArray(oQuery.getColumnfragid());  
		
		if(arFragmentId != null) {
			double fragmentImportance = 1.0/arFragmentId.length;
			
			for(i=0;i<arFragmentId.length;i++) {
				hmFragmentRank.put(arFragmentId[i], fragmentImportance);
			}
		}
		
		arFragmentId = Util.convertStringToLongArray(oQuery.getTablefragid());  
		
		if(arFragmentId != null) {
			double fragmentImportance = 1.0/arFragmentId.length;
			
			for(i=0;i<arFragmentId.length;i++) {
				hmFragmentRank.put(arFragmentId[i], fragmentImportance);
			}
		}
		
		Double importance;
		arFragmentId = Util.convertStringToLongArray(oQuery.getWherefragid());  
		
		if(arFragmentId != null) {
			double fragmentImportance = 1.0/arFragmentId.length;
			
			for(i=0;i<arFragmentId.length;i++) {
				if((importance = hmFragmentRank.get(arFragmentId[i])) != null) {
					importance += fragmentImportance;
				}
				hmFragmentRank.put(arFragmentId[i], fragmentImportance);
			}
		}
		
		arFragmentId = Util.convertStringToLongArray(oQuery.getGroupfragid());  
		
		if(arFragmentId != null) {
			double fragmentImportance = 1.0/arFragmentId.length;
			
			for(i=0;i<arFragmentId.length;i++) {
				if((importance = hmFragmentRank.get(arFragmentId[i])) != null) {
					importance += fragmentImportance;
				}
				hmFragmentRank.put(arFragmentId[i], fragmentImportance);
			}
		}
		
		arFragmentId = Util.convertStringToLongArray(oQuery.getHavingfragid());  
		
		if(arFragmentId != null) {
			double fragmentImportance = 1.0/arFragmentId.length;
			
			for(i=0;i<arFragmentId.length;i++) {
				if((importance = hmFragmentRank.get(arFragmentId[i])) != null) {
					importance += fragmentImportance;
				}
				hmFragmentRank.put(arFragmentId[i], fragmentImportance);
			}
		}
		
		
		return hmFragmentRank;
		
	}
}
